package com.futurelight.recipes.repository.remote;

import com.futurelight.recipes.model.FoodRecipes;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class RemoteRecipesRepository {
    private static RemoteRecipesRepository instance;
    private String baseUrl = "https://api.spoonacular.com/";
    private RecipesApi api;

    public Call<FoodRecipes> fetchFoodRecipes(String type) {
        return api.fetchFoodRecipes(type);
    }

    private RemoteRecipesRepository() {
        api = new Retrofit
                .Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(RecipesApi.class);
    }

    interface RecipesApi{
        /**
         * 按菜谱类型获取菜谱列表
         * @param type
         * @return
         */
        @GET("recipes/complexSearch?apiKey=15959de7f8df4ba1b9f2673014490580&fillIngredients=true&addRecipeInformation=true")
        Call<FoodRecipes> fetchFoodRecipes(@Query("type") String type);
    }


    /**
     * 单例获取网络获取食谱的仓库
     * @return
     */
    public static RemoteRecipesRepository getInstance() {
        if (instance == null) {
            synchronized (RemoteRecipesRepository.class) {
                if (instance == null) {
                    instance = new RemoteRecipesRepository();
                }
            }
        }
        return instance;
    }
}
